﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using ColorApplication.Helper;
using System.Configuration;

namespace ColorApplication.color
{
    public partial class Attributesfamilies : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Checking whether Admin or Not. AdminID key is in web.config
            if (Request.QueryString["id"] != null && Request.QueryString["id"] != "" && Request.QueryString["id"] == ConfigurationManager.AppSettings["AdminID"].ToString())
            {
                Clear(); 
            }
            else
            {
                Response.Redirect("~/Default.aspx");
            }
        }

        public void Clear()
        {
            // Clear Error And Success label Every Post Bak
            SuccesLabel.Text = "";
            ErrorLabel.Text = "";

        }

        protected void AttributefamiliesGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                // Change Deatils view Mode to edit
                AttributefamiliesDetailsView.ChangeMode(DetailsViewMode.Edit);
            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();
            }
        }

        protected void AttributefamiliesGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            try
            {
                // Bind java Script for Are you Sure to Delete Confirm Box
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    LinkButton delinkbutton = (LinkButton)e.Row.FindControl("DeleteLinkButton");
                    delinkbutton.OnClientClick = "return getDeleteMessage()";
                    HyperLink AttributeHyperLink = (HyperLink)e.Row.FindControl("AttributeHyperLink");

                    AttributeHyperLink.NavigateUrl += "&id=" + Request.QueryString["id"];

                }
            }
            catch (Exception err)
            {

                ErrorLabel.Text = err.Message.ToString();
            }
        }

        protected void AttributefamiliesDetailsView_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
        {
            try
            {
                // Show Success Message 
                // Grid data Binding
                SuccesLabel.Text = "Record updated successfully.";
                AttributefamiliesDetailsView.ChangeMode(DetailsViewMode.Insert);
                AttributefamiliesGridView.DataBind();
            }
            catch (Exception err)
            {

                ErrorLabel.Text = err.Message.ToString();
            }
        }

        protected void AttributefamiliesDetailsView_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
            try
            {
                // Show Success Message 
                // Grid data Binding
                SuccesLabel.Text = "Record inserted successfully.";
                AttributefamiliesDetailsView.ChangeMode(DetailsViewMode.Insert);
                AttributefamiliesGridView.DataBind();
            }
            catch (Exception err)
            {

                ErrorLabel.Text = err.Message.ToString();
            }
        }

        protected void AttributefamiliesGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                int AttributefamiliesId = Convert.ToInt32(e.Keys["Id"].ToString());

                // This Function Check Record in AttributesXAttributesfamiliese Table
                if (CheckAttributesfamiliesInAttributesXAttributesfamilies(AttributefamiliesId) == true)
                {
                    ErrorLabel.Text = "This Arrtibute Families is used in Attribute X Attributesfamilies.Delete is not allowed.";
                    e.Cancel = true;
                    //Cancel And Show Error Message
                }


            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();
            }
        }


        public bool CheckAttributesfamiliesInAttributesXAttributesfamilies(int AttributesfamiliesId)
        {
            // This Function Check AttributesfamiliesId in AttributesXAttributesfamilies Table
            // if Record Found Return true
            //else return false

            DataSet ds = new DataSet();
            using (SqlConnection con = new SqlConnection())
            {
                SqlParameter[] parameters = { new SqlParameter("@AttributesfamiliesId", AttributesfamiliesId) };

                SqlCommand cmd = SQLHelper.GetCommand(con, "CheckAttributesfamiliesInAttributesXAttributesfamilies", parameters);
                con.Open();

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                con.Close();

            }

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                return true;

            }

            return false;

        }

        protected void AttributefamiliesDetailsView_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
        {
            try
            {
                string oldDescription = e.OldValues["Description"].ToString();
                string newDescription = e.NewValues["Description"].ToString();

                if (oldDescription != newDescription)
                {
                    // This Function Check Record in Attribute Family for Dublicate Description name exist or Not
                    if (CheckDescriptionExistinAttributesfamilies(newDescription) == true)
                    {
                        ErrorLabel.Text = "Description already exists. Duplicate record is not allowed.";
                        e.Cancel = true;
                        //Cancel And Show Error Message
                    }

                }

            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();
            }
        }

        protected void AttributefamiliesDetailsView_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {
            try
            {
                string Description = e.Values["Description"].ToString();

                //  This Function Check Record in Attribute Family for Dublicate Description name exist or Not
                if (CheckDescriptionExistinAttributesfamilies(Description) == true)
                {
                    ErrorLabel.Text = "Description already exists. Duplicate record is not allowed.";
                    e.Cancel = true;
                    //Cancel And Show Error Message
                }


            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();
            }
        }

        public bool CheckDescriptionExistinAttributesfamilies(string Description)
        {
            // This Function Check Record in Attributesfamilies Table For Dublicate Description Name Exist or Not
            // if Record Found Return true
            //else return false

            DataSet ds = new DataSet();
            using (SqlConnection con = new SqlConnection())
            {
                SqlParameter[] parameters = { new SqlParameter("@Description", Description) };

                SqlCommand cmd = SQLHelper.GetCommand(con, "CheckDescriptionExistinAttributesfamilies", parameters);
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                con.Close();
            }

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                return true;
            }

            return false;

        }
    }
}